package com.fast.server.controller;


import com.fast.server.config.session.SessionWebUserConfig;
import com.fast.server.entity.constants.Constants;
import com.fast.server.entity.pages.UserPageParams;
import com.fast.server.entity.responseVO.RespBean;
import com.fast.server.entity.vo.SysUser;
import com.fast.server.exception.BusinessException;
import com.fast.server.service.ISysUserService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * <p>
 * 用户信息表 前端控制器
 * </p>
 *
 * @author ziyuancheng
 * @since 2024-05-05
 */
@RestController
@RequestMapping("/account")
public class SysUserController {

    @Autowired
    private ISysUserService userService;

    @GetMapping("/users")
    public RespBean getAllUsersByPage(@RequestBody UserPageParams pageParams) {
        return userService.getAllUsersByPage(pageParams);
    }

    @PostMapping("/user")
    @ApiOperation("新增用户")
    public RespBean addUser(@RequestBody SysUser user) {
        return userService.addUser(user);
    }

    @RequestMapping("/register")
    @ApiOperation("用户注册")
    public RespBean register(HttpSession session, String email, String nickName, String password, String checkCode, String emailCode) {
        try {
            if (!checkCode.equalsIgnoreCase((String) session.getAttribute(Constants.CHECK_CODE_KEY))) {
                throw new BusinessException("图片验证码不正确");
            }
            userService.register(email, nickName, password, emailCode);
            return RespBean.success();
        } finally {
            session.removeAttribute(Constants.CHECK_CODE_KEY);
        }
    }

    /**
     * @Description:
     */
    @RequestMapping("/login")
    @ApiOperation("用户登录")
    public RespBean login(HttpSession session, String email, String password, String checkCode) {
        try {
            if (!checkCode.equalsIgnoreCase((String) session.getAttribute(Constants.CHECK_CODE_KEY))) {
                throw new BusinessException("图片验证码不正确");
            }
            SessionWebUserConfig sessionWebUserConfig = userService.login(email, password);
            session.setAttribute(Constants.SESSION_KEY, sessionWebUserConfig);
            return RespBean.success(sessionWebUserConfig);
        } finally {
            session.removeAttribute(Constants.CHECK_CODE_KEY);
        }
    }


    @RequestMapping("/resetPwd")
    public RespBean resetPwd(HttpSession session, String email, String password, String checkCode, String emailCode) {
        try {
            if (!checkCode.equalsIgnoreCase((String) session.getAttribute(Constants.CHECK_CODE_KEY))) {
                throw new BusinessException("图片验证码不正确");
            }
            userService.resetPwd(email, password, emailCode);
            return RespBean.success();
        } finally {
            session.removeAttribute(Constants.CHECK_CODE_KEY);
        }
    }


    @PostMapping("/users")
    @ApiOperation("批量新增用户")
    public RespBean addUsers(@RequestBody List<SysUser> userList) {
        return userService.addUsers(userList);
    }

    @GetMapping("/user/{userId}/avatar")
    @ApiOperation("获取用户头像")
    public RespBean getUserAvatar(@PathVariable String userId) {
        return userService.getUserAvatar(userId);
    }

    @GetMapping("/user/{userId}/space")
    @ApiOperation("获取用户空间")
    public RespBean getUserSpace(@PathVariable String userId) {
        return userService.getUserSpace(userId);
    }

    @PutMapping("/user/{userId}/avatar")
    @ApiOperation("更新用户头像")
    public RespBean updateUserAvatar(@PathVariable String userId, @RequestParam String avatarUrl) {
        return userService.updateUserAvatar(userId, avatarUrl);
    }

    @PostMapping("/qq/login")
    @ApiOperation("QQ登录")
    public RespBean qqLogin(@RequestParam String qqOpenId) {
        return userService.qqLogin(qqOpenId);
    }

    @GetMapping("/qq/callback")
    @ApiOperation("QQ登录回调")
    public RespBean qqLoginCallback(@RequestParam String code) {
        return userService.qqLoginCallback(code);
    }

    @GetMapping("/user/{userId}")
    @ApiOperation("根据用户ID查询用户信息")
    public RespBean getUserById(@PathVariable String userId) {
        SysUser sysUser = userService.getUserById(userId);
        return RespBean.success(sysUser);
    }

    @PutMapping("/user/{userId}")
    @ApiOperation("根据用户ID修改用户信息")
    public RespBean updateUserById(@PathVariable String userId, @RequestBody SysUser sysUser) {
        return userService.updateUserById(userId, sysUser);
    }

    @DeleteMapping("/user/{userId}")
    @ApiOperation("根据用户ID删除用户")
    public RespBean deleteUserById(@PathVariable String userId) {
        return userService.deleteUserById(userId);
    }

    @GetMapping("/user")
    @ApiOperation("根据邮箱查询用户信息")
    public RespBean getUserByEmail(@RequestParam String email) {
        SysUser sysUser = userService.getUserByEmail(email);
        return RespBean.success(sysUser);
    }

    @PutMapping("/user")
    @ApiOperation("根据邮箱修改用户信息")
    public RespBean updateUserByEmail(@RequestParam String email, @RequestBody SysUser sysUser) {
        return userService.updateUserByEmail(email, sysUser);
    }

    @DeleteMapping("/user")
    @ApiOperation("根据邮箱删除用户")
    public RespBean deleteUserByEmail(@RequestParam String email) {
        return userService.deleteUserByEmail(email);
    }

    @GetMapping("/user/qqopenid")
    @ApiOperation("根据QQ OpenID查询用户")
    public RespBean getUserByQQOpenId(@RequestParam String qqOpenId) {
        SysUser sysUser = userService.getUserByQQOpenId(qqOpenId);
        return RespBean.success(sysUser);
    }

    @PutMapping("/user/qqopenid")
    @ApiOperation("根据QQ OpenID修改用户")
    public RespBean updateUserByQQOpenId(@RequestParam String qqOpenId, @RequestBody SysUser sysUser) {
        return userService.updateUserByQQOpenId(qqOpenId, sysUser);
    }

    @DeleteMapping("/user/qqopenid")
    @ApiOperation("根据QQ OpenID删除用户")
    public RespBean deleteUserByQQOpenId(@RequestParam String qqOpenId) {
        return userService.deleteUserByQQOpenId(qqOpenId);
    }

    @GetMapping("/user/wechatid")
    @ApiOperation("根据微信ID查询用户")
    public RespBean getUserByWechatId(@RequestParam String wechatId) {
        SysUser sysUser = userService.getUserByWechatId(wechatId);
        return RespBean.success(sysUser);
    }

    @PutMapping("/user/wechatid")
    @ApiOperation("根据微信ID修改用户")
    public RespBean updateUserByWechatId(@RequestParam String wechatId, @RequestBody SysUser sysUser) {
        return userService.updateUserByWechatId(wechatId, sysUser);
    }

    @DeleteMapping("/user/wechatid")
    @ApiOperation("根据微信ID删除用户")
    public RespBean deleteUserByWechatId(@RequestParam String wechatId) {
        return userService.deleteUserByWechatId(wechatId);
    }


}
